//购物车控制层
app.controller('orderInfoController',function($scope,orderInfoService,cartService){

    //获取地址列表
    $scope.findAddressList=function(){
        orderInfoService.findAddressList().success(
            function(response){
                $scope.addressList=response;
                //设置默认地址
                for(var i=0;i< $scope.addressList.length;i++){
                    if($scope.addressList[i].isDefault=='1'){
                        $scope.address=$scope.addressList[i];
                        break;
                    }
                }

            }
        );
    };
    //选择地址
    $scope.selectAddress=function(address){
        $scope.address=address;
    }
    //判断是否是当前选中的地址
    $scope.isSelectedAddress=function(address){
        if($scope.address==address){
            return true;
        }
        return false;
    }

    $scope.order={paymentType:'1'};
    //选择支付方式
    $scope.selectPayType=function(type){
        $scope.order.paymentType= type;
    }
    //查询购物车列表
    $scope.findCartList=function(){
        cartService.findCartList().success(
            function(response){
                $scope.cartList=response;
                $scope.totalValue= cartService.sum($scope.cartList);
            }
        );
    }

    //保存订单
    $scope.submitOrder=function(){
        //设置收货地址信息
        $scope.order.receiverAreaName=$scope.address.address;
        //手机
        $scope.order.receiverMobile=$scope.address.mobile;
        //联系人
        $scope.order.receiver=$scope.address.contact;
        orderInfoService.submitOrder( $scope.order ).success(
            function(response){
                if(200==response.code){
                    //页面跳转
                    if($scope.order.paymentType=='1'){
                        //如果是支付宝支付，跳转到支付页面
                        window.location.href = "http://localhost:8087/home-order-pay.html";
                    }else{
                        //如果货到付款，跳转到提示页面
                        location.href="paysuccess.html";
                    }
                }else{
                    alert(response.object);	//也可以跳转到提示页面
                }
            }
        );
    }

});